package com.zhny.aiot.control.cabinet.plugin.factory.imp;

import com.zhny.aiot.control.cabinet.plugin.ControlCabinetConst;
import com.zhny.aiot.control.cabinet.plugin.factory.IControlCabinetFactory;
import com.zhny.aiot.control.cabinet.plugin.model.ControlCabinetStoreEntry;
import com.zhny.aiot.plugin.modbus.device.ModemDevice;
import com.zhny.aiot.plugin.modbus.handler.model.ModemResponseMessage;
import io.netty.buffer.ByteBuf;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class CheckFactoryImpl implements IControlCabinetFactory {
    private static final Logger logger = LoggerFactory.getLogger(CheckFactoryImpl.class);
    @Override
    public int getCode() {
        return ControlCabinetConst.CHECK;
    }

    @Override
    public ControlCabinetStoreEntry getStoreEntry(ModemResponseMessage modemResponseMessage, ModemDevice device) {
        ByteBuf buf = modemResponseMessage.getData();
        int code = modemResponseMessage.getFunction();
        int len = modemResponseMessage.getSize();
        if(getCode() == code) {
            if(len == 88) {
                ControlCabinetStoreEntry storeEntry = getFlow(len, buf);
                if(storeEntry != null) {
                    device.save(storeEntry);
                    device.saveReal(storeEntry);
                }
                return storeEntry;
            }
            else if(len == 32) {
                ControlCabinetStoreEntry storeEntry = getPressureAndAdjustValue(len, buf);
                if(storeEntry != null) {
                    device.save(storeEntry);
                    device.saveReal(storeEntry);
                }
                return storeEntry;
            }
        }
        return null;
    }

    private ControlCabinetStoreEntry getFlow(int len,ByteBuf buf){
        ControlCabinetStoreEntry storeEntry = new ControlCabinetStoreEntry();
        if(len == 88) {
            logger.info("control-cabinet check flow start decoder");
            Float flow1=buf.readFloat();
            Float flowSpeed1=buf.readFloat();
            Float percentage1=buf.readFloat();
            Float flowPercentage1=buf.readFloat();
            int totalFlowHig1=((buf.readByte() << 24) &0xff000000|
                    (buf.readByte() << 16) &0x00ff0000|
                    (buf.readByte() << 8) &0x0000ff00 |
                    (buf.readByte()) & 0x000000ff);
            Float totalFlowLo1=buf.readFloat();
            logger.info("flow1 [flow:{}],[flowSpeed:{}],[totalFlow:{}]",flow1,flowSpeed1,totalFlowHig1 + totalFlowLo1);
            buf.skipBytes(20);
            Float flow2=buf.readFloat();
            Float flowSpeed2=buf.readFloat();
            Float percentage2=buf.readFloat();
            Float flowPercentage2=buf.readFloat();
            int totalFlowHig2=((buf.readByte() << 24) &0xff000000|
                    (buf.readByte() << 16) &0x00ff0000|
                    (buf.readByte() << 8) &0x0000ff00 |
                    (buf.readByte()) & 0x000000ff);
            Float totalFlowLo2=buf.readFloat();
            logger.info("flow2 [flow:{}],[flowSpeed:{}],[totalFlow:{}]",flow2,flowSpeed2,totalFlowHig2+totalFlowLo2);
            buf.skipBytes(20);
            storeEntry.setFlow1(flow1);
            storeEntry.setFlowSpeed1(flowSpeed1);
            storeEntry.setTotalFlow1(totalFlowHig1 + totalFlowLo1);
            storeEntry.setFlow2(flow2);
            storeEntry.setFlowSpeed2(flowSpeed2);
            storeEntry.setTotalFlow2(totalFlowHig2 + totalFlowLo2);
            return storeEntry;
        }

        return null;
    }

    private ControlCabinetStoreEntry getPressureAndAdjustValue(int len,ByteBuf buf){
        ControlCabinetStoreEntry storeEntry = new ControlCabinetStoreEntry();
        if(32 == len) {
            Float pressure1=buf.readFloat();
            Float pressure2=buf.readFloat();
            Float pressure3=buf.readFloat();
            Float pressure4=buf.readFloat();
            Float pressure5=buf.readFloat();
            Float pressure6=buf.readFloat();
            Float adjValve1 = buf.readFloat();
            Float adjValve2 = buf.readFloat();
            storeEntry.setPressure1(pressure1);
            storeEntry.setPressure2(pressure2);
            storeEntry.setPressure3(pressure3);
            storeEntry.setPressure4(pressure4);
            storeEntry.setPressure5(pressure5);
            storeEntry.setPressure6(pressure6);
            storeEntry.setAdValve1status(adjValve1);
            storeEntry.setAdValve2status(adjValve2);
            return storeEntry;
        }
        return null;
    }
}
